package main

import (
	"os"
	"strconv"
	"strings"
"fyne.io/fyne/v2/canvas"
"fmt"
"github.com/go-vgo/robotgo"
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/widget"
	"github.com/fyne-io/fyles/example"
	"github.com/fyne-io/fyles/example/curr"
	"github.com/fyne-io/fyles/example/galaxy"
	"github.com/fyne-io/fyles/example/primes"
	"github.com/fyne-io/fyles/example/thelist"
	"github.com/fyne-io/fyles/game"
	"github.com/fyne-io/fyles/hz2py"
	"github.com/fyne-io/fyles/linedisp"
	"github.com/fyne-io/fyles/odile"
"github.com/go-resty/resty/v2"
)

func MainMenuu(a fyne.App) *fyne.MainMenu {
	return fyne.NewMainMenu(
		fyne.NewMenu("Tools",

			fyne.NewMenuItem("Env", func() {
				envs := os.Environ()
				envList := widget.NewList(func() int {
					return len(envs) + 1
				},
					func() fyne.CanvasObject {
						return container.NewGridWithColumns(2,
							widget.NewLabel(""), widget.NewLabel(""))
					}, func(lii widget.ListItemID, co fyne.CanvasObject) {
						grid := co.(*fyne.Container)
						if lii == 0 {
							btn1 := widget.NewButton("Name", nil)
							btn2 := widget.NewButton("Value", nil)
							grid.Objects[0] = btn1
							grid.Objects[1] = btn2
							grid.Refresh()
							// lbl1.SetText("Name")
							// lbl2.SetText("Value")
							return
						}
						lbl1 := grid.Objects[0].(*widget.Label)
						lbl2 := grid.Objects[1].(*widget.Label)
						lbl1.SetText(strconv.Itoa(lii) + " - " + strings.Split(envs[lii-1], "=")[0])
						lbl2.SetText(strings.Split(envs[lii-1], "=")[1])
					})
				w1 := a.NewWindow("Env")
				w1.Resize(fyne.NewSize(800, 700))
				w1.SetContent(envList)
				w1.Show()
				defer w1.Close()
			}),
			fyne.NewMenuItem("linedisp", func() {
				linedisp.UI()
			}),
			fyne.NewMenuItem("Croc", func() {
				odile.UI(a)
			}),
			fyne.NewMenuItem("Hz2py", func() {
				hz2py.UI(a)
			}),
			fyne.NewMenuItem("Galaxy", func() {
				galaxy.UI(a)
			}),
		),

		fyne.NewMenu("Game",
			fyne.NewMenuItem("sudok", func() {
				game.UI(a)
				//sudok.UI(a)
			}),
		),
		fyne.NewMenu("example",
			fyne.NewMenuItem("curr", func() {
				curr.UI(a)
			}),
			fyne.NewMenuItem("primes", func() {
				primes.UI(a)
			}),
			fyne.NewMenuItem("pretty json", func() {
				example.PrettyJson(a)
			}),
			fyne.NewMenuItem("pinhole", func() {
				example.PinHole(a)
			}),
			fyne.NewMenuItem("random", func() {
				example.Random(a)
			}),
			fyne.NewMenuItem("mandelbrot", func() {
				example.Mandelbrot(a)
			}),
			fyne.NewMenuItem("gotable", func() {
				example.Gotable(a)
			}),
			fyne.NewMenuItem("now", func() {
				example.Now(a)
			}),
			fyne.NewMenuItem("Inflection", func() {
				example.Inflection(a)
			}),
			fyne.NewMenuItem("Ulexer", func() {
				example.Ulexer(a)
			}),
			fyne.NewMenuItem("TheList", func() {
				thelist.Me(a)
			}),
		),
restyUI(a),
	)

}

func restyUI(a fyne.App) *fyne.Menu {
	return fyne.NewMenu("other",
		fyne.NewMenuItem("resty", func() {
			w1 := a.NewWindow("resty")
			w1.Resize(fyne.NewSize(800, 700))
			entry := widget.NewEntry()
			entry.SetText("https://httpbin.org/get")
			entry.SetPlaceHolder("please input a url")
			result := widget.NewMultiLineEntry()
			btn := widget.NewButton("do", func() {
				// Create a Resty Client
				client := resty.New()

				resp, err := client.R().
					EnableTrace().
					Get(entry.Text)
				if err != nil {
					result.SetText("request error")
					return

				}
				result.SetText(resp.String())

			})
			top := container.NewVBox(entry, btn)
			cc := container.NewBorder(top, nil, nil, nil, result)

			w1.SetContent(cc)
			w1.Show()
			defer w1.Close()
		}),
		fyne.NewMenuItem("robotgo", func() {
			w1 := a.NewWindow("robotgo")
			w1.Resize(fyne.NewSize(800, 700))
			entry := widget.NewEntry()
			fpidEntry := widget.NewEntry()
			result := widget.NewMultiLineEntry()
			img1 := canvas.NewImageFromImage(nil)
			img1.FillMode = canvas.ImageFillOriginal
			btn := widget.NewButton("do", func() {
				result.FocusGained()
				go func() {
					for {
						x, y := robotgo.GetMousePos()
						color := robotgo.GetPixelColor(x, y)
						result.SetText(fmt.Sprint("pos: ", x, y, "\ncolor:", color))
						robotgo.MilliSleep(100)
					}
				}()

				sx, sy := robotgo.GetScreenSize()
				entry.SetText(fmt.Sprint("get screen size: ", sx, sy))

				bit := robotgo.CaptureScreen(10, 10, 400, 400)
				defer robotgo.FreeBitmap(bit)

				img := robotgo.ToImage(bit)
				img1.Image = img
				img1.Refresh()

				fpid, err := robotgo.FindIds("go")
				if err == nil {
					fpidEntry.SetText(fmt.Sprint("pids...", fpid))
				}
			})
			top := container.NewVBox(entry,fpidEntry, btn)
			cc := container.NewBorder(top, nil, nil, nil,
				container.NewGridWithColumns(2, img1, result))

			w1.SetContent(cc)
			w1.Show()
			defer w1.Close()
		}),
MnMachineInfo(a),
	)
}
func MnMachineInfo(a fyne.App) *fyne.MenuItem {
	return fyne.NewMenuItem("MachineInfo", func() {
		w1 := a.NewWindow("Env")

		w1.Resize(fyne.NewSize(800, 700))

		cc := widget.NewAccordion(
			widget.NewAccordionItem("CPU", getCpuInfo()),
		)
		w1.SetContent(cc)
		w1.Show()
		defer w1.Close()
	})
}